package com.baidu.aip

import android.app.Activity
import android.content.ContentValues
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.baidu.aip.utils.PathUtils

/**
 *@packageName com.baidu.aip
 *@author kzcai
 *@date 2021/7/6
 */
class CheckToPhotoActivity:Activity() {

    var fileUri:Uri?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        openCamera()
    }

    private fun openCamera() {
        val intent = Intent("android.media.action.IMAGE_CAPTURE")
        val values = ContentValues(1)
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
        fileUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri)
        startActivityForResult(intent, 300)
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 300 && resultCode == RESULT_OK) {
//            filePath = UriUtils.getPath(getContext(), fileUri);
//            startRegister(filePath);
            try {
                var  filePath = PathUtils.getPath(this, fileUri)
                startRegister(filePath)
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(this,"图片获取失败",Toast.LENGTH_SHORT).show()
                finish()
            }
        }else if (requestCode==100) {
            if (resultCode== RESULT_OK){
                val data = data!!.getByteArrayExtra("data")
                val intent1 = Intent()
                intent1.putExtra("data", data)
                setResult(RESULT_OK, intent1)
                finish()
            }
        }
    }


    private fun startRegister(file: String) {
        val it = Intent(this, RegisterFaceActivity::class.java)
        val bundle = Bundle()
        bundle.putString("imagePath", file)
        it.putExtras(bundle)
        startActivityForResult(it, 100)
    }

}